<?php

namespace App\Http\Controllers\Download;

use App\Common\Exception\ErrorException;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class TemplateController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
    * @Description: Excel模板文件下载
    * @Author: kzj
    * @datetime: 2022-05-12
    */
    public function excelDownload()
    {
        $file_name = request()->get('file_name','');  //真实文件名
        $show_name = request()->get('show_name','');  //展示文件名
        
        if ($file_name == ''){
            throw new ErrorException('文件不存在');
        }

        $headers=[
            "Content-Disposition"=>"attachment; filename=".$show_name,
            "Content-Transfer-Encoding"=>" binary",
            "Content-Type"=> $this->get_content_type($file_name)
        ];

        return response()->download(public_path('download/template/excel/' . $file_name), $show_name, $headers);
    }

    /**
    * @Description: Excel文件
    * @Author: kzj
    * @datetime: 2022-05-12
    */
    public function excelExport()
    {
        $file_name = request()->get('file_name','');  //真实文件名
        $show_name = request()->get('show_name','');  //展示文件名

        if ($file_name == ''){
            throw new ErrorException('文件不存在');
        }

        $headers=[
            "Content-Disposition"=>"attachment; filename=".$show_name,
            "Content-Transfer-Encoding"=>" binary",
            "Content-Type"=> $this->get_content_type($file_name)
        ];
        $file_name = str_replace(env('APP_URL'),'',$file_name);

        return response()->download(public_path($file_name), $show_name, $headers)->deleteFileAfterSend(true);
    }

    private function get_content_type($filename)
    {
        $mime_types = array(
            'txt' => 'text/plain',
            'htm' => 'text/html',
            'html' => 'text/html',
            'php' => 'text/html',
            'css' => 'text/css',
            'js' => 'application/javascript',
            'json' => 'application/json',
            'xml' => 'application/xml',
            'swf' => 'application/x-shockwave-flash',
            'flv' => 'video/x-flv',

            // images
            'png' => 'image/png',
            'jpe' => 'image/jpeg',
            'jpeg' => 'image/jpeg',
            'jpg' => 'image/jpeg',
            'gif' => 'image/gif',
            'bmp' => 'image/bmp',
            'ico' => 'image/vnd.microsoft.icon',
            'tiff' => 'image/tiff',
            'tif' => 'image/tiff',
            'svg' => 'image/svg+xml',
            'svgz' => 'image/svg+xml',

            // archives
            'zip' => 'application/zip',
            'rar' => 'application/x-rar-compressed',
            'exe' => 'application/x-msdownload',
            'msi' => 'application/x-msdownload',
            'cab' => 'application/vnd.ms-cab-compressed',

            // audio/video
            'mp3' => 'audio/mpeg',
            'qt' => 'video/quicktime',
            'mov' => 'video/quicktime',

            // adobe
            'pdf' => 'application/pdf',
            'psd' => 'image/vnd.adobe.photoshop',
            'ai' => 'application/postscript',
            'eps' => 'application/postscript',
            'ps' => 'application/postscript',

            // ms office
            'doc' => 'application/msword',
            'rtf' => 'application/rtf',
            'xls' => 'application/vnd.ms-excel',
            'ppt' => 'application/vnd.ms-powerpoint',

            // open office
            'odt' => 'application/vnd.oasis.opendocument.text',
            'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
        );

        $ext = strtolower(pathinfo($filename,PATHINFO_EXTENSION ));

        if (array_key_exists($ext, $mime_types)) {
            return $mime_types[$ext];
        } else {
            return 'application/octet-stream';
        }
    }

    /**
     * 导出教师课程表
     * @return \Symfony\Component\HttpFoundation\BinaryFileResponse
     * @throws ErrorException
     */
    public function excelTimetableExport(Request $request)
    {
        $url = $request->query('url');
        if ($url == ''){
            throw new ErrorException('excel文件生成失败');
        }
        $filename = pathinfo($url,PATHINFO_BASENAME);
        $headers=[
            "Content-Disposition"=>"attachment; filename=".$filename,
            "Content-Transfer-Encoding"=>" binary",
            "Content-Type"=> 'application/vnd.ms-excel'
        ];
        $userName = auth()->user()->user_uuid;
        $path = public_path(). '/download/timetable/'.$userName.'/'.$filename;
        return response()->download($path,$filename,$headers)->deleteFileAfterSend();
    }

    /**
     * @Description: zip文件下载
     * @Author: kzj
     * @datetime: 2022-08-12
     */
    public function zipDownload()
    {
        $real_name = request()->get('file_name');
        $url = public_path('download/zip/') . $real_name;
        $ext = strtolower(pathinfo($real_name, PATHINFO_EXTENSION));
        if (strtolower($ext) == 'zip'){
            $content_type = "application/x-zip-compressed";
        }else{
            throw new ErrorException('格式不正确');
        }

        $headers=[
            "Content-Disposition"=>"attachment;filename=" . $real_name,
            "Content-Transfer-Encoding"=>" binary",
            "Content-Type"=> $content_type
        ];
        if (!file_exists($url)){
            throw new ErrorException('文件生成失败!');
        }

        return response()->download($url,$real_name,$headers)->deleteFileAfterSend();
    }
}
